<?php
header("content-Type: text/html; charset=UTF-8");
class zhifuqrControl extends BaseBuyControl {

    public function __construct() {
        parent::__construct();
        Language::read('home_cart_index');
    }

    function zhifuqrOp(){
        Tpl::output('buy_step','step3');

        $order_info = Model()->table('order')->where(array('pay_sn' => $_GET['trade_no']))->find();

        if(!empty($order_info)){
            if($order_info['order_state'] != 10){
                showMessage('该订单不是未付款状态，无法支付！','/shop/index.php?act=member_order','html','error');
            }
            Tpl::output('order_sn',$order_info['order_sn']);
            Tpl::showpage('buy_zhifuqr');
            exit;
        }
        showMessage('支付失败，请联系网站管理员','/shop/index.php?act=member_order','html','error');
    }

    public function zhifuqrOrderOp(){

        $m = Model();
        $zhifuqr = $m->table('payment')->where(array('payment_code' => 'zhifuqr'))->find();
        $zhifuqr_conf = unserialize($zhifuqr['payment_config']);
        $merchant_private_key = preg_replace('/\s*/', '', trim($zhifuqr_conf['private_key']));

        if(in_array($_POST['order_type'],array('real_order','vr_order','pd_order'))){
            if(md5($_POST['trade_no'].$_POST['order_type'])){
                $order_type = $_POST['order_type'];
            }else{
                exit('参数异常');
            }
        }else{
            exit('参数异常');
        }



        $order_info = Model('order')->getOrderInfo([
            'pay_sn' => $_POST['trade_no']
        ], ['order_goods']);

        $signStr= "";
        $product_name = "";

        foreach($order_info['extend_order_goods'] as $k => $v){
            if($k==0){
                $product_name .= $v['goods_name'];
            }else{
                $product_name .= '_'.$v['goods_name'];
            }
        }
        $parms = array(
            'sign'              => '',
            'merchant_code'     => trim($zhifuqr_conf['merchant_code']),
            'service_type'      => "weixin_scan",
            'interface_version' => "V3.1",
            'sign_type'         => "RSA-S",
            'notify_url'        => "http://".$_SERVER['HTTP_HOST']."/shop/api/payment/zhifuqr/notify_url.php",
            'order_no'          => $order_info['order_sn'],
            'order_time'        => date( 'Y-m-d H:i:s' ,$order_info['add_time']),
            'order_amount'      => $order_info['order_amount'],
            'product_name'      => $product_name,
            'extra_return_param'=> $order_type.'__'.$order_info['pay_sn'],
            'extend_param'      => 'customer_name^'.$order_info['receiver_name'].'|customer_idNumber^'.$order_info['idcard'],
            'client_ip'         => getIp()
        );

        $signStr = $signStr."client_ip=".$parms['client_ip']."&";
        if($parms['extend_param']!=''){
            $signStr = $signStr."extend_param=".$parms['extend_param']."&";
        }
        if($parms['extra_return_param']!=''){
            $signStr = $signStr."extra_return_param=".$parms['extra_return_param']."&";
        }
        $signStr = $signStr."interface_version=".$parms['interface_version']."&";
        $signStr = $signStr."merchant_code=".$parms['merchant_code']."&";
        $signStr = $signStr."notify_url=".$parms['notify_url']."&";
        $signStr = $signStr."order_amount=".$parms['order_amount']."&";
        $signStr = $signStr."order_no=".$parms['order_no']."&";
        $signStr = $signStr."order_time=".$parms['order_time']."&";
        $signStr = $signStr."product_name=".$parms['product_name']."&";
        $signStr = $signStr."service_type=".$parms['service_type'];

        $merchant_private_key = "-----BEGIN PRIVATE KEY-----"."\r\n".wordwrap(trim($merchant_private_key),64,"\r\n",true)."\r\n"."-----END PRIVATE KEY-----";

        $merchant_private_key= openssl_get_privatekey($merchant_private_key);

        openssl_sign($signStr,$sign_info,$merchant_private_key,OPENSSL_ALGO_MD5);

        $sign = base64_encode($sign_info);

        $parms['sign'] = $sign;

        $postdata = array(
            'extend_param'=>        $parms['extend_param'],
            'extra_return_param'=>  $parms['extra_return_param'],
            'merchant_code'=>       $parms['merchant_code'],
            'service_type'=>        $parms['service_type'],
            'notify_url'=>          $parms['notify_url'],
            'interface_version'=>   $parms['interface_version'],
            'sign_type'=>           $parms['sign_type'],
            'order_no'=>            $parms['order_no'],
            'client_ip'=>           $parms['client_ip'],
            'sign'=>                $parms['sign'],
            'order_time'=>          $parms['order_time'],
            'order_amount'=>        $parms['order_amount'],
            'product_name'=>        $parms['product_name']
        );

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,"https://api.dinpay.com/gateway/api/scanpay");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response=curl_exec($ch);
        $res=simplexml_load_string($response);

        curl_close($ch);

        if($res->response->resp_code=='SUCCESS'){
            echo $response;
        }else{
            return false;
        }


    }


}
